package com.fbs.job.admin.core.config;

import com.fbs.job.admin.core.alarm.JobAlarmer;
import com.fbs.job.admin.core.entity.FbsJobLog;
import com.fbs.job.admin.core.scheduler.FbsJobScheduler;
import com.fbs.job.admin.mapper.*;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Component
public class FbsJobAdminConfig implements InitializingBean, DisposableBean {

    private static FbsJobAdminConfig adminConfig = null;
    public static FbsJobAdminConfig getAdminConfig() {
        return adminConfig;
    }
    @Value("${fbs.job.accessToken}")
    private String accessToken;
    @Value("${fbs.job.triggerpool.fast.max}")
    private int triggerPoolFastMax;
    @Value("${fbs.job.triggerpool.slow.max}")
    private int triggerPoolSlowMax;
    @Value("${fbs.job.logretentiondays}")
    private int logretentiondays;

    @Resource
    private FbsJobRegistryMapper fbsJobRegistryMapper;
    @Resource
    private FbsJobActuatorMapper fbsJobActuatorMapper;
    @Resource
    private FbsJobInfoMapper fbsJobInfoMapper;
    @Resource
    private FbsJobLogMapper fbsJobLogMapper;

    @Resource
    private FbsJobLogReportMapper fbsJobLogReportMapper;

    @Resource
    private DataSource dataSource;
    @Resource
    private JobAlarmer jobAlarmer;


    private FbsJobScheduler fbsJobScheduler;


    public int getLogretentiondays() {
        return logretentiondays;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public FbsJobRegistryMapper getFbsJobRegistryMapper() {
        return fbsJobRegistryMapper;
    }

    public FbsJobActuatorMapper getFbsJobActuatorMapper() {
        return fbsJobActuatorMapper;
    }

    public FbsJobInfoMapper getFbsJobInfoMapper() {
        return fbsJobInfoMapper;
    }

    public FbsJobLogMapper getFbsJobLogMapper() {
        return fbsJobLogMapper;
    }

    public FbsJobLogReportMapper getFbsJobLogReportMapper() {
        return fbsJobLogReportMapper;
    }

    public int getTriggerPoolFastMax() {
        if (triggerPoolFastMax < 200) {
            return 200;
        }
        return triggerPoolFastMax;
    }
    public int getTriggerPoolSlowMax() {
        if (triggerPoolSlowMax < 100) {
            return 100;
        }
        return triggerPoolSlowMax;
    }
    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        adminConfig = this;
        fbsJobScheduler = new FbsJobScheduler();
        fbsJobScheduler.init();
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public JobAlarmer getJobAlarmer() {
        return jobAlarmer;
    }
}
